﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DeadlockDetection
{   
    public class Resources
    {
        public Resource[] resources;
        private Random random = new Random();

        public int Length
        {
            get
            { return resources.Length; }
        }

        public Resource this[int x]
        {
            get { return resources[x]; }
        }

        // wiele instancji danego zasobu
        public Resources(int numberOfResources, int maxInstances)
        {
            resources = new Resource[numberOfResources];
            for (int i = 0; i < numberOfResources; i++)
            {
                resources[i] = new Resource(i, random.Next(1, maxInstances));
            }                
        }

        // pojedyncze zasoby
        public Resources(int numberOfResources)
        {
            resources = new Resource[numberOfResources];
            for (int i = 0; i < numberOfResources; i++)
            {
                resources[i] = new Resource(i);
            }
        }

        // wczytywanie instancji z pliku
        public Resources(StreamReader instanceFile, int numberOfResources)
        {
            string buf = " ";
            while(!buf.StartsWith("resources"))
            {
                buf = instanceFile.ReadLine();    
            }
            buf = instanceFile.ReadLine();
            buf.Trim();
            string[] totalFields = buf.Split(' ');

            buf = instanceFile.ReadLine();
            buf.Trim();
            string[] availableFields = buf.Split(' ');

            resources = new Resource[numberOfResources];

            for (int i = 0; i < numberOfResources; i++)
            {
                resources[i] = new Resource(i, Convert.ToInt32(totalFields[i]), Convert.ToInt32(availableFields[i]));
            }
        }
    }
}
